home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / ACL / Animation Class Library / Headers / AnimBase.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  10.4 KB  |  308 lines  |  [TEXT/MPCC]

  1.  
  2. /********************************************
  3.  **** Animation Class Library V1.0 © 1994 Yves Schmid & Alia Development
  4.  ****
  5.  **** AnimBase.h
  6.  ****
  7.  **** Created:      12 May 1994
  8.  **** Modified:     02 Septembre 1994
  9.  **** Version:      0
  10.  **** Compatible:   C++, Mac System 7
  11.  ****
  12.  **** Description:  AnimBase is the central object of the animation system, it handles 
  13.  ****                timing, drawing, buffer and background.
  14.  ****
  15.  ****                AnimBase is a child class of the AnimSupervisor class.
  16.  ****
  17.  *******************/
  18.  
  19.  
  20. #ifndef AnimBase_H
  21. #define AnimBase_H
  22.  
  23.  
  24. #include "AnimGfx.h"
  25. #include "Anim.h"
  26. #include "AnimPict.h"
  27. #include "AnimMask.h"
  28. #include "AnimCollision.h"
  29.  
  30. typedef  Boolean (*AnimCollisionFunc)(AnimObject*,AnimObject*);
  31.  
  32. class AnimBase: public AnimSupervisor
  33. {
  34.  
  35.     //***********************************************************
  36.     //.............. P U B L I C   M E T H O D S.................
  37.  
  38.     public:
  39.     
  40.     AnimBase(void);
  41.     ~AnimBase(void);
  42.     
  43.  
  44.     virtual void preload_framedef(AnimFrameDef *def);     // This method preloads
  45.                                                         // all datas of an AnimFrameDef in
  46.                                                         // memory. But the Anim is not built.
  47.     
  48.     virtual Boolean update(void);    // If the tupdate has ellapsed starts to refresh
  49.                                     // the display. It draws all animations, background
  50.                                     // buffer, sorts anims, etc...
  51.                                     // The most common way to use this method is to call
  52.                                     // it repeatly in your event loop.
  53.                                     // Returns TRUE if the update happends.
  54.                                     
  55.     virtual void updatewindow(void);    // Call this method when your window need to be
  56.                                         // updated.
  57.  
  58.     
  59.  
  60.     // If you call this method the next update will refresh the full screen. It is useful
  61.     // when you draw something in the background AnimGfx.
  62.  
  63.     inline void forcefullrefresh(void) {needupdatefull=TRUE;}
  64.  
  65.     
  66.     // Gets/sets your custom collision function. To stop collision destection simply
  67.     // set the collision function to NULL:
  68.     // "anim->setcollisionfunc(NULL);"
  69.                                     
  70.     inline AnimCollisionFunc getcollisionfunc(void) const {return collisionfunc;}    
  71.     inline void setcollisionfunc(const AnimCollisionFunc f) {collisionfunc = f;}    
  72.     
  73.  
  74.     
  75.     inline short getsortflags(void)        // These methods allow you set/get the sort flags
  76.         {return sortflags;}                // of animation objects under the control of
  77.                                         // this AnimBase. See sort modes.
  78.     inline void setsortflags(short f)    
  79.         {sortflags = f; if (f) sort();}
  80.  
  81.     
  82.  
  83.     inline void settupdate(const long t) {tupdate = t;}
  84.     inline long gettupdate(void) const {return tupdate;}  
  85.                                                     // These methods allow you set/get the
  86.                                                     // number of ticks to wait before
  87.                                                     // updating the display. By default
  88.                                                     // tupdate is initialised to 3
  89.                                                     // ticks. To have the maximum speed of
  90.                                                     // the computer set this
  91.                                                     // value to 0. Most of the time
  92.                                                     // it is not a good thing to set tupdate
  93.                                                     // to zero because your
  94.                                                     // animation may be too fast 
  95.                                                     // if you use different
  96.                                                     // computers. 
  97.     
  98.     void installbackground(long    resID);
  99.     void installbackground(AnimGfx *back);        // Installs a background picture. You have to create
  100.                                                 // an AnimGfx. The AnimGfx will not be freed by
  101.                                                 // this object (unless you allocate the AnimGfx
  102.                                                 // with "newgfx"). Do not delete your AnimGfx
  103.                                                 // before removing it from the AnimBase!
  104.                                                 // You don't have to call "removebackground" before
  105.                                                 // installing a new background.
  106.  
  107.     void removebackground(void);                // removes the background
  108.     inline AnimGfx *getbackground() {return background;}
  109.  
  110.     void buildbuffer(short width, short height);    // Builds an offscreen buffer to stop flickering
  111.                                                     // on screen. You set your buffer size to the same size
  112.                                                     // of your visual animation region in your window. If
  113.                                                     // you have a background installed, use
  114.                                                     // "buildbufferback" to have a buffer which
  115.                                                     // have the same size of your background.
  116.                                                     // You don't have to call removebuffer before
  117.                                                     // installing a new buffer of a different size.
  118.     
  119.     void removebuffer(void);                        // removes buffer
  120.  
  121.     void buildbufferback(void);        // Same as "buildbuffer" but uses the background image to find
  122.                                     // the size of the buffer. If there is no background this
  123.                                     // method does nothing.
  124.  
  125.     virtual AnimGfx *newgfx(long resid, Boolean maybepurged =FALSE);    
  126.                                     // Builds an AnimGfx using a pict resource id. The AnimGfx
  127.                                     // is added to an internal list of the AnimBase so you
  128.                                     // don't have to worry about deleting the object. AnimBase
  129.                                     // keeps trace of the resource IDs for each created
  130.                                     // images, so it never duplicates a picture in memory.
  131.                                     // If "newgfx" finds that AnimBase has an AnimGfx 
  132.                                     // in memory which comes from the same resource, 
  133.                                     // it returns the previous AnimGfx
  134.                                     // without allocating a new one.
  135.                                     // If "maybepurged" is TRUE this picture may be deleted
  136.                                     // by "deleteunusedgfx" else this picture may only be delete
  137.                                     // by yourself or when you delete this AnimBase.
  138.                                     // A mask is created for the AnimGfx.
  139.  
  140.                                     
  141.     virtual void deleteunusedgfx(void);    // This method frees all graphics which are not used
  142.                                         // anymore in this AnimBase. The function checks all 
  143.                                         // animations and background. If a picture is not used in 
  144.                                         // animations or background the picture will be
  145.                                         // deleted. Note that this method will free AnimGfx
  146.                                         // that you allocate yourself using "newgfx" only if 
  147.                                         // you pass TRUE to "maybepurged". Currently this method is
  148.                                         // never called by the Anim Class Library. It's up to you
  149.                                         // to call it when you think there are unused graphics
  150.                                         // loaded in memory.
  151.  
  152.  
  153.     inline short getbasex(void) const {return basex;}    // Gets/sets basex and basey
  154.     inline short getbasey(void) const {return basey;}
  155.     inline void setbasex(const short x) {basex = x;}
  156.     inline void setbasey(const short y) {basey = y;}
  157.  
  158.     AnimBase *getanimbase(void); // Returns "this"
  159.  
  160.  
  161.  
  162.  
  163.     //***********************************************************
  164.  
  165.     //..........................................................
  166.     // You should not call the following methods!
  167.  
  168.     virtual void docollision(void);    
  169.     virtual void sort(void);        
  170.  
  171.  
  172.     void exstartrefbuild(void);
  173.     void exaddrefreshrect(Rect *r);
  174.     void exobtainresult(Rect **rtab, long *tabsiz);
  175.     void exupdateaddrect(Rect &r);
  176.  
  177.  
  178.     //.....................
  179.  
  180.     protected:
  181.     Boolean                needupdatefull;    
  182.     Boolean                nopartialupdate;
  183.  
  184.     //.....................
  185.  
  186.     protected:
  187.  
  188.     virtual void updatebackground(void);
  189.  
  190.     inline AnimGfx *getbuffer(void) {return buffer;}
  191.  
  192.  
  193.     private:
  194.  
  195.     short    basex,basey;            
  196.     
  197.  
  198.     AnimGfx *background;                    
  199.  
  200.     AnimGfx *buffer;                
  201.     
  202.     long      tupdate;                
  203.     long      lasttupdate;            
  204.  
  205.     short      sortflags;            
  206.  
  207.     AnimCollisionFunc collisionfunc;    
  208.  
  209.  
  210.     // Private methods
  211.     AnimObject *findsupcollision(AnimObject *anim);
  212.     AnimObject *findnextcollision(AnimObject *anim);
  213.     Boolean loopcollision2(AnimObject *anim, AnimObject *coanim);
  214.     Boolean loopcollision1(AnimObject *anim);
  215.  
  216.  
  217.     //.....................................
  218.     // Used by the refresh system. Private.
  219.  
  220.     Boolean                refreshing;
  221.     long                refreshnused[4];
  222.     long                refreshstacksize;
  223.     Rect                *refreshstack[4];
  224.     short                currentstack;
  225.  
  226.     long addrefreshrect(long rstack);
  227.     void purgeallrefreshrect(long rstack);
  228.     void newrefreshrect(Rect *newr, long rstack);
  229.     void processrefresh(long rstack, AnimGfx *source);
  230.     void deleterefreshrect(unsigned long delp, long rstack);
  231.     void unionallrefresh(unsigned long newp, long rstack);
  232. };
  233.  
  234.  
  235. //********** Sort Modes *************
  236.  
  237. // • These flags can be ORed:
  238.  
  239. #define    ASORT_NULL            0            // Don't sort animations.
  240.  
  241. #define ASORT_PRIORITY        0x0100        // Uses the priority field of the anim class.
  242.                                         // The higher priority is drawn in front of
  243.                                         // others.
  244.                                         // If this flag is ORed with an exclusive flag,
  245.                                         // it is first checked. If priority is equal
  246.                                         // then AnimBase checks the exclusive flag.
  247.  
  248. // • Following flags are exclusive (but they can be ORed with previous!).
  249.  
  250. #define ASORT_BOTTOMRIGHT    1        // Animations which have the higher vertical position
  251.                                     // are in front of the others. If two animations have
  252.                                     // the same vertical positions, it's the higher horizontal
  253.                                     // position which is draw in front of the other.
  254.  
  255. #define ASORT_BOTTOMLEFT    2        // Animations which have the higher vertical position
  256.                                     // are in front of the others. If two animations have
  257.                                     // the same vertical positions, it's the smaller horizontal
  258.                                     // position which is draw in front of the other.
  259.  
  260. #define ASORT_TOPRIGHT        3        // Animations which have the smaller vertical position
  261.                                     // are in front of the others. If two animations have
  262.                                     // the same vertical positions, it's the higher horizontal
  263.                                     // position which is drawn in front of the other.
  264.  
  265. #define ASORT_TOPLEFT        4        // Animations which have the smaller vertical position
  266.                                     // are in front of the others. If two animations have
  267.                                     // the same vertical positions, it's the smaller horizontal
  268.                                     // position which is drawn in front of the other.
  269.  
  270. #define ASORT_RIGHTBOTTOM    5        // Animations which have the higher horizontal position
  271.                                     // are in front of the others. If two animations have
  272.                                     // the same horizontal positions, it's the higher vertical
  273.                                     // position which is drawn in front of the other.
  274.  
  275. #define ASORT_RIGHTTOP        6        // Animations which have the higher horizontal position
  276.                                     // are in front of the others. If two animations have
  277.                                     // the same horizontal positions, it's the smaller vertical
  278.                                     // position which is drawn in front of the other.
  279.  
  280. #define ASORT_LEFTBOTTOM    7        // Animations which have the smaller horizontal position
  281.                                     // are in front of the others. If two animations have
  282.                                     // the same horizontal positions, it's the higher vertical
  283.                                     // position which is drawn in front of the other.
  284.  
  285. #define ASORT_LEFTTOP        8        // Animations which have the smaller horizontal position
  286.                                     // are in front of the others. If two animations have
  287.                                     // the same horizonal positions, it's the smaller vertical
  288.                                     // position which is drawn in front of the other.
  289.  
  290.  
  291.  
  292. //***********************
  293. // These commands and structures are for internal usage, you should not have to use them
  294.  
  295. #define    animcmd_ISANIMGFXUSED        950
  296.  
  297. struct acmdstruct_Searchagfxused
  298. {
  299.     AnimGfx    *agfx;
  300.     Boolean    used;
  301. };
  302.  
  303. #define    animcmd_FRAMEDELETED        951
  304.  
  305.  
  306. #endif
  307.  
  308.